import java.io.*;
import java.util.Scanner;

public class demo16 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要复制的文件路径：");
        String srcPath = scan.next();
        System.out.println("请输入要复制到的目标文件路径：");
        String destPath = scan.next();

        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        if(!srcFile.exists()){
            System.out.println("输入要复制的文件路径不存在！");
            return;
        }
        if(srcFile.isDirectory()){
            System.out.println("输入要复制的文件路径有误，为目录路径1！");
            return;
        }
        if(!destFile.exists()){
            System.out.println("输入要复制到的目标文件路径不存在！");
            return;
        }
        if(destFile.isDirectory()){
            System.out.println("输入要复制到的目标文件路径有误，为目录路径2！");
            return;
        }
        if(destFile.exists()){
            System.out.println("目录路径已经存在，是否要进行覆盖？y/n");
            String choice= scan.next();
            if (!choice.toLowerCase().equals("y")) {
                System.out.println("停止复制");
                return;
            }
        }
//        D:\Image\img.png
//        D:\summary-ku\File\File\aaa\222

        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)){
            while(true){
                byte[] buffer = new byte[1024];
                int res = inputStream.read(buffer);
                if(res == -1){
                    break;
                }
                for (int i = 0; i < res; i++) {
                    outputStream.write(buffer[i]);
                }
            }
            outputStream.flush();


        }catch (IOException e){
            e.printStackTrace();
        }

    }
}
